// ewfcmd.cpp : Defines the entry point for the console application.
//
// Copyright (c) 2004 SJJ Embedded Micro Solutions, LLC
// All Rights Reserved
// Code is provided AS IS without any warranty
//
// Description: Command line application that demonstrates how to implement
//				the EWF APIs for three simple commands: Enable EWF, 
//				Disable EWF, and Commit changes to a protect volume.
//				Each command requires a manual reboot to take place for change to
//				to take effect. 
//
//				Shutdown & Reboot can happen three ways:
//				1. Using the FBA.exe application
//					c:\windows\fba>fba -shutdown
//					c:\windows\fba>fba -reboot
//				2. ExitWindowsEx API command
//				3. Using the XPe Power Management APIs (XPEPM)
//
//				Other EWF APIs are used to open and close handles to the protected volume
//				EwfMgrOpenProtected and EwfMgrClose
//
//  Note: For RAM overlays, hitting the system reset button will not commit changes 
//			to the protected volume. The system must be shutdown or restarted using 
//			one of the methods above.
//
//	Note2: EWFCMD assumes Disk and RAM overlays only. Modifications need to be made for
//			EWF RAM REG Overlay.
//
//


#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include "ewfapi.h"
void DisplayHelp();

int wmain(int argc, wchar_t* argv[])
{
	HANDLE hDisk;
	WCHAR szDrive[20];
	DWORD dwStatus = ERROR_SUCCESS;
	BOOL bResult =FALSE;
		
	wprintf(L"\nEWFCMD - EWF API Example\n");
	wprintf(L"Copyright (c) 2004 Sean Liming\n\n");
	
	// The IF statement holds the main body of the application. The IF statement 
	// checks to make sure that our command line arguments are true
	// If the command line arguments are wrong, then display help

	if (	(argc == 3)
		&&	(L'-' == argv[2][0]))
	{
	
		//Now lets open the handle to the volume and check if the volume is valid
		//First step is to put the drive parameter in the proper format: \\.\c:
		swprintf(szDrive, L"\\\\.\\%s", argv[1]);
		
		//Next, Open the handle and check for errors
		hDisk = EwfMgrOpenProtected(szDrive);
		if (INVALID_HANDLE_VALUE == hDisk) 
		{
			wprintf(L"Invalid Handle. Please make sure that the drive exists\n");
			DisplayHelp();
			return 1;
		}
		
		// The switch statement is based on the second parameter to carry out
		// the action to perform on the volume handle.
		switch (argv[2][1])
			{
			
			// E for Enable EWF - EwfMgrEnable
			case 'E':
				{
					
					bResult = EwfMgrEnable(hDisk);
					if (!bResult)
					{
						dwStatus = GetLastError();
						wprintf(L"EwfMgrEnable failed LE = %u\n",dwStatus);
						EwfMgrClose(hDisk);
						return 1;
					}
                    
					wprintf(L"EWF Enabled - ");
					wprintf(L"Reboot system for change to take effect\n");
					EwfMgrClose(hDisk);
				}
				break;
			
			// D for Disable EWF - EWFMgrDisable
			case 'D' :
				{
					//For EWF RAM REG configurations, the second a prameter (commit)
					//must be TRUE. This will commit the EWF RAM REg disable state,
					//which is required to disable the EWF RAM Reg configuration
					
					bResult = EwfMgrDisable(hDisk,FALSE);
					if (!bResult)
					{
						dwStatus = GetLastError();
						wprintf(L"EwfMgrDisable failed LE = %u\n",dwStatus);
						EwfMgrClose(hDisk);
						return 1;
					}
                    
					wprintf(L"EWF Disabled - ");
					wprintf(L"Reboot system for change to take effect\n");
					EwfMgrClose(hDisk);
				}
				break;
			
			// C for Commit changes to protected volume - EwfMgrCommit
			case 'C' :
				{
					bResult = EwfMgrCommit(hDisk);
					if (!bResult)
					{
						dwStatus = GetLastError();
						wprintf(L"EwfMgrCommit failed LE = %u\n",dwStatus);
						EwfMgrClose(hDisk);
						return 1;
					}
                    
					wprintf(L"Changes commited - ");
					wprintf(L"Reboot system for change to take effect\n");
					EwfMgrClose(hDisk);
				}
				break;

			default:
				DisplayHelp();
				return 1;
			}
	}
	else
	{
		// If the parameter conditions failes display help
		DisplayHelp();
		return 1;
	}

	return 0;
}
//The DisplayHelp funtion lists the Usage and options.
void DisplayHelp()
{
	wprintf(L"\nEWFCMD <Drive> <Option>\n");
	wprintf(L"-E Enable EWF\n");
	wprintf(L"-D Disable EWF\n");
	wprintf(L"-C Commit Changes to Protect Volume\n");
}
	
	
